package com.wgo.surveyModel.wiring;

import com.wgo.bpot.persist.db4object.Db4ObjectPersistService;
import com.wgo.bpot.server.persist.UniqueValue;
import com.wgo.bpot.server.persist.UniqueValueFactoryImpl;
import com.wgo.bpot.server.persist.trigger.TriggerRegistryImpl;
import com.wgo.bpot.wiring.ServiceAccessor;
import com.wgo.bpot.wiring.ServiceAccessorImpl;
import com.wgo.bpot.wiring.ServicesFactoryImpl;
import com.wgo.bpot.wiring.WiringEngine;
import com.wgo.bpot.wiring.db4o.Db4oDomainModelConvention;

public class SurveyModelWiring implements WiringEngine<ServiceAccessor> {
	private ServiceAccessor sa;

	public ServiceAccessor getServiceAccessor(Object client) {
		if (null == sa) {
			sa = createServiceAccessor();
		}
		return sa;
	}
	

//	private ServiceAccessorHiber createServiceAccessor() {
//		ServiceAccessorImpl sa = new ServiceAccessorImpl();
//		//PersistService ps = new Db4ObjectPersistService();
//		HibernatePersistService ps = new HibernatePersistService();
//		SurveyEjbModelConvention ejbConvention = new SurveyEjbModelConvention();
//        sa.setDomainModelConvention(ejbConvention);
//		ps.setDomainModelConvention(ejbConvention);
//		sa.setPersistService(ps);
//		sa.setTriggerRegistry(new TriggerRegistryImpl(ps));
//		UniqueValueFactoryImpl uniqueValueFactory = new UniqueValueFactoryImpl();
//		uniqueValueFactory.setUniqueValueClass(UniqueValue.class);
//		uniqueValueFactory.setPersistService(ps);
//		ps.setUniqueValueFactory(uniqueValueFactory);
//		sa.setInitializationService(new SurveyStartup(ps));
//        sa.setServicesFactory(new ServicesFactoryImpl(sa));
//		return sa;
//	}
	
	private ServiceAccessor createServiceAccessor() {
		ServiceAccessorImpl sa = new ServiceAccessorImpl();
		Db4ObjectPersistService ps = new Db4ObjectPersistService();
        sa.setDomainModelConvention(new SurveyDomainModelConvention());
		ps.setDomainModelConvention(sa.getDomainModelConvention());
		sa.setPersistService(ps);
		sa.setTriggerRegistry(new TriggerRegistryImpl(ps));
		UniqueValueFactoryImpl uniqueValueFactory = new UniqueValueFactoryImpl();
		uniqueValueFactory.setUniqueValueClass(UniqueValue.class);
		uniqueValueFactory.setPersistService(ps);
		ps.setUniqueValueFactory(uniqueValueFactory);
		sa.setInitializationService(null);
        sa.setServicesFactory(new ServicesFactoryImpl(sa));
		return sa;
	}

	public ServiceAccessor getServiceAccessor() {
		return getServiceAccessor(this);
	}

}
